package com.apes.scm.settle.settle.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.account.model.Cooperation;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SettleSimple.CONST_TABLE_NAME)
public class SettleSimple extends Model {
    public static final String CONST_TABLE_NAME = "acc_settle_simple";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "结算(简)单", prefix = "J")
    @Comment("结算单号")
    private String id; //结算单号

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_id", referencedColumnName = "id", nullable = false)
    @Comment("业务伙伴编码")
    private PartyRole partner; //业务伙伴

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司编码")
    private PartyRole company; //公司

    @Column(name = "settle_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("结算金额")
    private double settleAmount = 0; //结算金额

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cooperation_id", referencedColumnName = "id", nullable = false)
    @Comment("合作方式")
    private Cooperation cooperation; //合作方式

    @States(name = "结算状态", values = {
            @State(id = "sent", name = "结算申请"),
            @State(id = "approve", name = "已审核")})
    @Column(name = "state", length = 10, nullable = false)
    private String state; //状态

    @States(name = "开票状态", values = {
            @State(id = "pending", name = "待开票"),
            @State(id = "in", name = "开票中"),
            @State(id = "finish", name = "已开票")})
    @Column(name = "invoice_state", length = 10)
    private String invoiceState;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("部门编码")
    private Dept dept; //部门

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note; //备注

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUID; //审核

    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate; //审核日期

    @Column(name = "free_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("免收金额")
    private double freeAmount = 0; //免收金额

    @Column(name = "repletion_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("实结金额")
    private double repletionAmount = 0; //实结金额

    @Column(name = "invoiced_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("已开票金额")
    private double invoicedAmount = 0;


    @OneToMany(mappedBy = "settleSimple", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<SettleSimpleItem> settleSimpleItems = new HashSet(); // 付款方式明细

    public Set<SettleSimpleItem> getSettleSimpleItems() {
        if (settleSimpleItems == null)  settleSimpleItems = new HashSet();
        for (SettleSimpleItem item : settleSimpleItems) {
            item.setSettleSimple(this);
        }
        return settleSimpleItems;
    }

    @OneToMany(mappedBy = "settleSimple", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<SettleSimplePaymentMethod> paymentMethodSet = new HashSet(); // 付款方式明细

    public Set<SettleSimplePaymentMethod> getPaymentMethodSet() {
        if (paymentMethodSet  == null ) paymentMethodSet = new HashSet();
        for (SettleSimplePaymentMethod item : paymentMethodSet) {
            item.setSettleSimple(this);
        }
        return paymentMethodSet;
    }
}
